package com.xxx.yuedong.mall.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.mall.controller.form.*;
import com.xxx.yuedong.mall.model.pojo.Category;
import com.xxx.yuedong.mall.model.vo.CategoryVO;
import com.xxx.yuedong.mall.service.CategoryService;
import com.xxx.yuedong.mine.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * 描述：     目录Controller
 * @author hc
 */
@RestController
@Api(tags = "商品类别相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    /**
     * 后台添加目录
     */
    @ApiOperation(value = "后台添加商品类别目录")
    @PostMapping("admin/category/add")
    public R addCategory(@Valid @RequestBody AddCategoryForm form) {
//        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
//        if (currentUser == null) {
//            return R.error();
//        }
//        //校验是否是管理员
//        boolean adminRole = userService.checkAdminRole(currentUser);
//        if (adminRole) {
//            //是管理员，执行操作
            categoryService.add(form);
            return R.ok();
//        } else {
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
//        }
    }

    @ApiOperation(value = "后台更新商品类别目录")
    @PostMapping("admin/category/update")
    public R updateCategory(@Valid @RequestBody UpdateCategoryForm form) {
//        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
//        if (currentUser == null) {
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
//        }
//        //校验是否是管理员
//        boolean adminRole = userService.checkAdminRole(currentUser);
//        if (adminRole) {
            //是管理员，执行操作
            Category category = new Category();
            BeanUtils.copyProperties(form, category);
            categoryService.update(category);
            return R.ok();
//        } else {
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
//        }
    }

    @ApiOperation(value = "后台删除商品类别目录")
    @PostMapping("admin/category/delete")
    public R deleteCategory(@Valid @RequestBody DeleteCategoryByIdForm form) {
        categoryService.delete(form.getIds());
        return R.ok();
    }

    @ApiOperation(value = "后台商品类别目录列表")
    @PostMapping("admin/category/list")
    public R listCategoryForAdmin(@Valid @RequestBody SearchCategoryListForm form) {
        PageInfo pageInfo = categoryService.listForAdmin(form.getPageNum(), form.getPageSize(), form.getName());
        return R.ok().put("page",pageInfo);
    }

    @ApiOperation(value = "根据类别ID查询后台商品类别")
    @PostMapping("admin/category/searchCategoryById")
    public R searchCategoryById(@Valid @RequestBody SearchCategoryByIdForm form) {
        Category category = categoryService.searchCategoryById(form.getId());
        return R.ok().put("category",category);
    }

    @ApiOperation(value = "前台商品类别目录列表")
    @GetMapping("category/list")
    public R listCategoryForCustomer() {
        List<CategoryVO> list = categoryService.listCategoryForCustomer(0);
        return R.ok().put("list",list);
    }
}
